<?php

class Menu extends fvUploaded implements iLogger {
    
    protected $currentEntity = '';
	
	function __construct () {
	    $this->currentEntity = __CLASS__;
        parent::__construct(fvSite::$fvConfig->get("entities.{$this->currentEntity}.fields"), 
                            fvSite::$fvConfig->get("entities.{$this->currentEntity}.table_name"), 
                            fvSite::$fvConfig->get("entities.{$this->currentEntity}.primary_key", "id"),
                            $this->currentEntity);
	}

    function validateName($value) {
        $valid = (strlen($value) > 0);
        $this->setValidationResult("name", $valid);
        return $valid;
    }

    function validateUrl($value) {
        $valid = (strlen($value) > 0);
        $this->setValidationResult("url", $valid);
        return $valid;
    }
    
    public function delete() {
        $childMenus = MenuManager::getInstance()->getByParentId($this->getPk());
        
        foreach ($childMenus as $childMenu) {
            $childMenu->parent_id = 0;
            $childMenu->save();
        }
        
        parent::delete();
    }
    
    public function getLogMessage($operation) {
        $message = "Пункт меню был ";
        switch ($operation) {
            case Log::OPERATION_INSERT: $message .= "создан ";break;
            case Log::OPERATION_UPDATE: $message .= "изменен ";break;
            case Log::OPERATION_DELETE: $message .= "удален ";break;
            case Log::OPERATION_ERROR: $message = "Произошла ошибка при операции с записью ";break;
        }
        $user = fvSite::$fvSession->getUser();
        $message .= "в ".date("Y-m-d H:i:s").". Менеджер [".$user->getPk()."] " . $user->getLogin() . " (" . $user->getFullName() . ")";
        return $message;    
    }
    
    public function getLogName() {
        return $this->name;    
    }
    
    public function putToLog($operation) {
        $logMessage = new Log();
        $logMessage->operation = $operation;
        $logMessage->object_type = __CLASS__;
        $logMessage->object_name = $this->getLogName();
        $logMessage->object_id = $this->getPk();
        $logMessage->manager_id = (fvSite::$fvSession->getUser())?fvSite::$fvSession->getUser()->getPk():-1;
        $logMessage->message = $this->getLogMessage($operation);
        $logMessage->edit_link = fvSite::$fvConfig->get('dir_web_root')."menus/?id=".$this->getPk();
        $logMessage->save();
    }  
    
    public function getImageField() {
        return 'image';
    }
    
    public function saveUploadFile($fieldName, $origFile) {
        parent::saveUploadFile($fieldName, $origFile);
        
        fvMediaLib::createThumbnail($this->getImageDir().$this[$fieldName], $this->getImageDir().$this->getImageName($fieldName, self::IMAGE_TYPE_THUMB), array('type' => self::IMAGE_TYPE_THUMB));
        fvMediaLib::createThumbnail($this->getImageDir().$this[$fieldName], $this->getImageDir().$this->getImageName($fieldName, self::IMAGE_TYPE_SMALL), array('type' => self::IMAGE_TYPE_SMALL));
    }
}

?>
